#coding=utf-8
from uliweb.orm import *
from uliweb.utils.common import get_var
from uliweb.i18n import ugettext_lazy as _
import cgi

def get_modified_user():
    from uliweb import request
    
    return request.user.id

class RiskPoint(Model):
    __verbose_name__ = u'风险点表'
    
    point_id = Field(str, max_length=10, verbose_name='风险点序号', required=True, index=True, unique=True)
    review = Field(CHAR, max_length=1, verbose_name='审核状态', choices=get_var('PARA/RISK_REVIEW_STATUS'))
    in_project = Field(bool, verbose_name='是否在整改项目中', choices=get_var('PARA/YESNO'))
    check_depart = Reference('kfzx', verbose_name='检查单位', collection_name='check_depart_rickpoints')
    risk_sys = Field(str, max_length=80, verbose_name='系统名称')
    check_num = Field(str, max_length=80, verbose_name='检查项编号')
    level = Field(CHAR, verbose_name='风险级别', max_length=1, choices=get_var('PARA/RISK_LEVEL'))
    status = Field(CHAR, max_length=1, verbose_name='风险点状态', choices=get_var('PARA/RISK_POINT_STATUS'))
    policy = Field(str, verbose_name='策略名称', max_length=255)
    target = Field(str, max_length=255, verbose_name='风险对象')
    has_monitor = Field(bool, verbose_name='是否有监控手段', choices=get_var('PARA/YESNO'))
    has_emergency = Field(bool, verbose_name='是否有技术应急措施', choices=get_var('PARA/YESNO'))
    has_drilled = Field(bool, verbose_name='应急措施是否经过有效演练', choices=get_var('PARA/YESNO'))
    has_tech = Field(bool, verbose_name='是否有技术应对手段', choices=get_var('PARA/YESNO'))
    busi_supervisor = Field(str, max_length=80, verbose_name='风险对应的主管业务部门')
    has_busi_solution = Field(bool, verbose_name='是否有业务应对手段', choices=get_var('PARA/YESNO'))
    has_busi_emergency = Field(bool, verbose_name='是否有业务应急措施', choices=get_var('PARA/YESNO'))
    has_auto_alarm = Field(bool, verbose_name='监控结果是否支持自动报警', choices=get_var('PARA/YESNO'))
    risk_desc = Field(TEXT, verbose_name='风险点描述')
    risk_analysis = Field(TEXT, verbose_name='风险分析')
    tech_control_desc = Field(TEXT, verbose_name='技术控制手段综述')
    busi_control_desc = Field(TEXT, verbose_name='业务控制手段综述')
    memo = Field(TEXT, verbose_name='备注')
    
    #extra fields
    system = Reference('system', verbose_name='任务所属系统')
    sub_sys = Field(str, verbose_name='子系统英文简称')
    requirement = Reference('requirement', verbose_name='准开发需求编号')
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    
#    class AddForm:
#        fields = [
#            {'name':'check_id', 'sortable':True, 'width':70},           
#            {'name':'risk_sys', 'width':40, 'sortable':True},
#            {'name':'system','width':60, 'sortable':True},
#            {'name':'sub_sys', 'width':40, 'sortable':True},
#            {'name':'risk_desc', 'sortable':True},
#        #          {'name':'risk_analysis', 'sortable':True},
#            {'name':'level', 'sortable':True, 'width':60},
#            {'name':'policy', 'width':120, 'sortable':True},
#            {'name':'requirement', 'width':80},
#            {'name':'task_id', 'width':100},
#            {'name':'plan_finish_date', 'width':85, 'sortable':True},
#        #          {'name':'reform_type', 'verbose_name':'关注级别', 'width':100, 'sortable':True},
#        #          {'name':'reform_measure', 'width':200, 'sortable':True},
#        #          {'name':'answer_policy', 'width':90, 'sortable':True},
#            {'name':'urgent_degree', 'width':85, 'sortable':True},
#            {'name':'lead_tx', 'width':90, 'sortable':True},
#            {'name':'kfzx', 'width':50, 'sortable':True},
#            {'name':'solution_mode', 'width':50, 'sortable':True},
#            {'name':'nondev_riskpoint_status', 'width':50, 'sortable':True},
#            {'name':'footnote', 'width':200},
#            {'name':'memo', 'width':200},
#            {'name':'modified_date', 'width':100},
#            {'name':'modified_user', 'width':100},
#        ]
        
    
#    class EditForm:
#        fields = ['review', 'review_result']
    
    class Table:
        fields = [
            {'name':'point_id', 'sortable':True, 'width':70},
            {'name':'review', 'width':60, 'sortable':True},
            {'name':'in_project','width':60, 'sortable':True},
            {'name':'check_depart', 'width':70, 'sortable':True},
            {'name':'risk_sys', 'sortable':True},
            {'name':'check_num', 'sortable':True},
            {'name':'level', 'sortable':True, 'width':60},
            {'name':'status', 'sortable':True, 'width':60},
            {'name':'policy', 'width':120, 'sortable':True},
            {'name':'target', 'width':120, 'sortable':True},
            {'name':'has_monitor', 'width':120, 'sortable':True},
            {'name':'has_emergency', 'width':120, 'sortable':True},
            {'name':'has_drilled', 'width':120, 'sortable':True},
            {'name':'has_tech', 'width':120, 'sortable':True},
            {'name':'busi_supervisor', 'width':120, 'sortable':True},
            {'name':'has_busi_solution', 'width':120, 'sortable':True},
            {'name':'has_busi_emergency', 'width':120, 'sortable':True},
            {'name':'has_auto_alarm', 'width':120, 'sortable':True},
            {'name':'risk_desc', 'width':120, 'sortable':True},
            {'name':'risk_analysis', 'width':120, 'sortable':True},
            {'name':'tech_control_desc', 'width':120, 'sortable':True},
            {'name':'busi_control_desc', 'width':120, 'sortable':True},
            {'name':'memo', 'width':200},
            {'name':'requirement', 'width':80},
            {'name':'system', 'width':100},
#            {'name':'requirement', 'width':100},
        ]
        
    def __unicode__(self):
        return u"风险点:%s" % (self.risk_desc)
    
    def get_url(self):
        return u'<a href="/riskpoint/%d/view" title="%s">%s</a>' % (self.id, cgi.escape(self.risk_desc[:40]), self.point_id)
    
class RiskSolution(Model):
    __verbose_name__ = u'风险点措施表'
    
    riskpoint = Reference('riskpoint', verbose_name='风险点', reference_fieldname='point_id', required=True)
    solution_id = Field(str, max_length=10, verbose_name='整改事项序号', required=True, index=True, unique=True)
    type = Field(str, max_length=255, verbose_name='整改类型')
    policy = Field(CHAR, max_length=1, verbose_name='风险应对策略', choices=get_var('PARA/RISK_SOLU_POLICY'))
    urgency = Field(CHAR, max_length=1, verbose_name='整改紧迫程度', choices=get_var('PARA/RISK_SOLU_URGENCY'))
    plan_finish_date = Field(datetime.date, verbose_name='计划完成时间')
    finish_date = Field(datetime.date, verbose_name='实际完成时间')
    zx_ammount = Field(float, verbose_name='行内技术人员工作量')
    gs_ammount = Field(float, verbose_name='所需合作公司工作量')
    
    lead_depart = Reference('kfzx', verbose_name='牵头方', collection_name='lead_depart_risksolutions')
    tech_lead_depart = Reference('kfzx', verbose_name='技术部门主责方', collection_name='teck_lead_depart_risksolutions', required=True)
    tech_co_depart = Field(str, max_length=255, verbose_name='技术部门协同方')
    busi_lead_depart = Field(str, max_length=80, verbose_name='业务部门主责方')
    busi_co_depart = Field(str, max_length=255, verbose_name='业务部门协同方')
    yw_ammount = Field(float, verbose_name='业务人员工作量')
    solution = Field(TEXT, verbose_name='整改措施')
    solution_additional_explan = Field(TEXT, verbose_name='整改措施补充说明')
    result = Field(TEXT, verbose_name='整改效果')
    memo = Field(TEXT, verbose_name='备注')
    itsm_num = Field(str, max_length=10, verbose_name='ITSM单号')
    
    #extra fields
    solution_mode  = Field(CHAR, max_length=1, verbose_name='解决方式', choices=get_var('PARA/SOLUTION_MODE'),default='1')
    nondev_riskpoint_status = Field(CHAR, max_length=1, verbose_name='整改状态', choices=get_var('PARA/NONDEV_RISKPOINT_STATUS'))
    task_id = Reference('tasks_develop', verbose_name='执行任务')
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    
    class Table:
        fields = [
            {'name':'riskpoint', 'sortable':True, 'width':70},
            {'name':'solution_id', 'sortable':True, 'width':50},
            {'name':'type', 'sortable':True, 'width':100},
            {'name':'policy', 'sortable':True, 'width':50},
            {'name':'urgency', 'sortable':True, 'width':50},
            {'name':'plan_finish_date', 'sortable':True, 'width':70},
            {'name':'finish_date', 'sortable':True, 'width':70},
            {'name':'zx_ammount', 'sortable':True, 'width':70},
            {'name':'gs_ammount', 'sortable':True, 'width':70},
            {'name':'tech_lead_depart', 'sortable':True, 'width':70},
            {'name':'tech_co_depart', 'sortable':True, 'width':70},
            {'name':'lead_depart', 'sortable':True, 'width':70},
            {'name':'busi_lead_depart', 'sortable':True, 'width':70},
            {'name':'busi_co_depart', 'sortable':True, 'width':70},
            {'name':'yw_ammount', 'sortable':True, 'width':70},
            {'name':'solution', 'sortable':True, 'width':120},
            {'name':'solution_additional_explan', 'sortable':True, 'width':120},
            {'name':'result', 'sortable':True, 'width':120},
            {'name':'solution_mode', 'sortable':True, 'width':70},
            {'name':'nondev_riskpoint_status', 'sortable':True, 'width':70},
            {'name':'task_id', 'sortable':True, 'width':70},
            {'name':'itsm_num', 'sortable':True, 'width':70},
            {'name':'memo', 'sortable':True, 'width':100},
            {'name':'modified_date', 'sortable':True, 'width':80},
            {'name':'modified_user', 'sortable':True, 'width':80},
            {'name':'review', 'verbose_name':'风险点信息/审核状态','width':60},
            {'name':'in_project','verbose_name':'风险点信息/是否在整改项目中','width':60},
            {'name':'check_depart', 'verbose_name':'风险点信息/检查单位','width':70},
            {'name':'risk_sys',  'verbose_name':'风险点信息/系统名称','width':60},
            {'name':'check_num',  'verbose_name':'风险点信息/检查项编号','width':60},
            {'name':'level',  'verbose_name':'风险点信息/风险级别','width':60},
            {'name':'status', 'verbose_name':'风险点信息/风险点状态','width':60},
            {'name':'risk_policy', 'verbose_name':'风险点信息/策略名称','width':120},
            {'name':'target', 'verbose_name':'风险点信息/风险对象','width':120},
            {'name':'has_monitor', 'verbose_name':'风险点信息/是否有监控手段','width':120},
            {'name':'has_emergency', 'verbose_name':'风险点信息/是否有技术应急措施','width':120},
            {'name':'has_drilled', 'verbose_name':'风险点信息/应急措施是否经过有效演练','width':120},
            {'name':'has_tech', 'verbose_name':'风险点信息/是否有技术应对手段','width':120},
            {'name':'busi_supervisor', 'verbose_name':'风险点信息/风险对应的主管业务部门','width':120},
            {'name':'has_busi_solution', 'verbose_name':'风险点信息/是否有业务应对手段','width':120},
            {'name':'has_busi_emergency', 'verbose_name':'风险点信息/是否有业务应急措施','width':120},
            {'name':'has_auto_alarm', 'verbose_name':'风险点信息/监控结果是否支持自动报警','width':120},
            {'name':'risk_desc', 'verbose_name':'风险点信息/风险点描述','width':120},
            {'name':'risk_analysis', 'verbose_name':'风险点信息/风险分析','width':120},
            {'name':'tech_control_desc', 'verbose_name':'风险点信息/技术控制手段综述','width':120},
            {'name':'busi_control_desc', 'verbose_name':'风险点信息/业务控制手段综述','width':120},
            {'name':'risk_memo', 'verbose_name':'风险点信息/备注','width':200},
            {'name':'requirement', 'verbose_name':'风险点信息/准开发需求编号','width':80},
            {'name':'system', 'verbose_name':'风险点信息/任务所属系统','width':100},
            ]
            
    def __unicode__(self):
        return u"整改措施:%s" % (self.solution)

    def get_url(self):
        return u'<a href="/risksolution/%d/view" title="%s">%s</a>' % (self.id, self.solution, self.solution)
